﻿using System.Collections.Generic;
using System.IO;

namespace CopyRightCodeCollector {

    public static class FileRangeIndicator {

        public static bool MatchExtension(string filepath, string extension) {
            var ext = Path.GetExtension(filepath);
            return ext == extension;
        }

        public static bool MatchDirectory(string filepath, string directory) {
            var name = Path.GetFileName(filepath);
            if (name != directory)
                return false;

            return Directory.Exists(filepath);
        }

        public static bool MatchSubPath(string filepath, string path) {
            return filepath.Contains(path);
        }

        public static bool MatchRule(string filepath, string rule) {
            var extensionRule = rule[0] == '.';
            var dirRule = rule[^1] == '/';

            if (extensionRule && dirRule)
                return MatchExtension(filepath, rule) && MatchDirectory(filepath, rule[0..^1]);
            if (extensionRule)
                return MatchExtension(filepath, rule);
            if (dirRule)
                return MatchDirectory(filepath, rule[0..^1]);
            return MatchSubPath(filepath, rule);
        }

        public static bool MatchAnyRule(string filepath, HashSet<string> rules) {
            foreach (string rule in rules) {
                if (MatchRule(filepath, rule))
                    return true;
            }
            return false;
        }
    }
}
